package com.breakfast.fun.ui.start;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.breakfast.fun.App;
import com.breakfast.fun.Constant;
import com.breakfast.fun.MainActivity;
import com.breakfast.fun.R;
import com.breakfast.fun.adapter.home.WelcomeAdapter;
import com.breakfast.fun.data.entity.home.CityData;
import com.breakfast.fun.ui.home.PermissionActivity;
import com.breakfast.fun.ui.other.city.CityActivity;
import com.breakfast.fun.util.SharedUtil;
import com.google.android.gms.analytics.Tracker;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class WelcomeActivity extends PermissionActivity {

    @Bind(R.id.vp_welcome)
    ViewPager vpWelcome;
    @Bind(R.id.ll_welcome)
    LinearLayout llWelcome;

    private List<View> viewList = new ArrayList<>();
    private ImageView[] imgList = null;
    private List<Integer> imgIdList = new ArrayList<>();

    private boolean isFirstEnter = true;

    private Tracker mTracker;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.activity_welcome);
        super.onCreate(savedInstanceState);
        mTracker = App.getApp().getDefaultTracker();

        isFirstEnter = SharedUtil.getBoolean(this, "firstEnter", true);
        if (!isFirstEnter) {
            Intent intent = new Intent(WelcomeActivity.this, StartActivity.class);
            startActivity(intent);
            finish();
            return;
        }


        ButterKnife.bind(this);

        SharedUtil.putBoolean(this, "firstEnter", false);
        imgIdList.add(R.drawable.bg_welcome1);
        imgIdList.add(R.drawable.bg_welcome2);
        imgIdList.add(R.drawable.bg_welcome3);
//        imgIdList.add(R.drawable.bg_welcome4);

        ImageView imageView;
        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        for (int i = 0; i < imgIdList.size(); i++) {
            imageView = new ImageView(this);
            imageView.setLayoutParams(mParams);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setImageResource(imgIdList.get(i));
            viewList.add(imageView);
        }

        viewList.get(viewList.size() - 1).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                SharedUtil.putBoolean(getApplicationContext(), "firstEnter",
                        false);
                CityData cityData = CityData.objectFromData(SharedUtil.getString(WelcomeActivity.this, "city"));
                if (cityData == null) {
                    Intent intent = new Intent(WelcomeActivity.this, CityActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Constant.setCityData(cityData);
                    Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                    intent.putExtra("isFirst", true);
                    startActivity(intent);
                    finish();
                }
            }
        });

        imgList = new ImageView[viewList.size()];
        LinearLayout.LayoutParams dParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        dParams.setMargins(0, 0, 10, 0);
        for (int i = 0; i < viewList.size(); i++) {
            imgList[i] = new ImageView(this);
            imgList[i].setLayoutParams(dParams);
            if (0 == i) {
                imgList[i].setBackgroundResource(R.drawable.ic_dot_focus);
            } else {
                imgList[i].setBackgroundResource(R.drawable.ic_dot_blur);
            }
            llWelcome.addView(imgList[i]);
        }
        vpWelcome.setAdapter(new WelcomeAdapter(viewList));
        vpWelcome.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < viewList.size(); i++) {
                    if (position == i) {
                        imgList[i].setBackgroundResource(R.drawable.ic_dot_focus);
                    } else {
                        imgList[i].setBackgroundResource(R.drawable.ic_dot_blur);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void initView() {

    }
}
